<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
    <title></title>
</head>
<body>
<table id="permissionmanage_treegrid" style="width:100%;"></table>
<shiro:hasPermission name="permission:add">
    <div id="permission_add_btn">
    <a href="${pageContext.request.contextPath}/admin/permission/add.html" class="easyui-linkbutton" iconCls="icon-add">添加权限</a>
    </div>
</shiro:hasPermission>
<script>
    top.setMenuActionOpen('系统设置', '权限管理');

    var permissionmanage = {
                gridInit: function () {
                        $('#permissionmanage_treegrid').treegrid({
                            url: '${pageContext.request.contextPath}/admin/permission/getManagePermissionsAjax.html',
                            idField: 'id',
                            treeField: 'name',
                            fitColumns: true,
                            striped: true,
                            method: 'post',
                            rownumbers: true,
                            singleSelect: true,
                            animate: true,
                            lines: true,
                            toolbar:'#permission_add_btn',
                            columns: [[
                                {title: '权限名字', field: 'name',width:'40%'},
                                {field: 'description', title: '权限描述',width:'40%'},
                                {
                                    field: 'action',
                                    title: '操作',
                                    hidden: true,
                                    width:'18%',
                                    formatter: function (value, row, index) {
                                        var _actionbtn = "";
                                        if (GlobalPermission.permission_add) {
                                            _actionbtn += "&nbsp;<a href='${pageContext.request.contextPath}/admin/permission/add.html?parentId=" + row.id + "' title='添加' >[添加]</a>&nbsp;";
                                        }
                                        if (GlobalPermission.permission_edit) {
                                            _actionbtn += "&nbsp;<a href='${pageContext.request.contextPath}/admin/permission/edit/" + row.id + ".html'  title='编辑' >[编辑]</a>&nbsp;";
                                        }
                                        if (GlobalPermission.permission_del) {
                                            _actionbtn += "&nbsp;<a href='javascript:void(0)' onclick='permissionmanage.deletePermission(" + row.id + ")' title='删除'>[删除]</a>&nbsp;";
                                        }
                                        return _actionbtn;
                                    }
                                }
                            ]],
                            loadFilter: function (data) {
                                var error = data.error;
                                if (error) {
                                    Common.error(error);
                                    return;
                                }
                                return data.success;
                            },
                            onLoadSuccess: function (data) {
                                if (GlobalPermission.permission_add || GlobalPermission.permission_edit || GlobalPermission.permission_del) {
                                    $('#permissionmanage_treegrid').treegrid('showColumn', 'action');
                                }
                            },
                            onLoadError: function () {
                                LoadError();
                            }
                        });
                },
                init: function () {
                    permissionmanage.gridInit();
                },
                deletePermission: function (id) {
                    var ids = new Array();
                    var children = $('#permissionmanage_treegrid').treegrid("getChildren", id);
                    $(children).each(function (i, v) {
                        ids.push(v.id);
                    });
                    ids.push(id);
                    Common.confirm("你确定要删除所选权限吗？", function (r) {
                        if (r) {
                            $.ajax({
                                url: '${pageContext.request.contextPath}/admin/permission/deletePermissionsAjax.html',
                                data: {"ids": ids},
                                dataType: 'json',
                                success: function (data) {
                                    var error = data.error;
                                    if (error) {
                                        Common.error(error);
                                        return;
                                    }
                                    Common.info(data.success);
                                    $('#permissionmanage_treegrid').treegrid("load");
                                }
                            });
                        }
                    })
                }
            }
            ;
    permissionmanage.init();
</script>
</body>
</html>
